package com.qiantu.phone.ui.fragment;

import static com.qiantu.phone.ui.fragment.device.BaseDeviceFragment.STATUS;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.qiantu.api.entity.MusiclikeListBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * @Description: 我喜欢  音乐列表
 * @Time:  2023年1月31日
 */
public class  MusiclikeListFragment extends AppFragment<AppActivity>{
    private String serialNo;
    private String dialog_sign;
    private RecyclerView music_like_recyclerview;
    private TextView music_num;
    private int count;  //当前播放歌曲下标
    private MusiclikelistAdapter musiclikelistAdapter;
    private boolean ifscene;
    private CheckBox checkbox_music;
    private ArrayList<MusiclikeListBean.MusiclikeList> musiclikeListBeans=new ArrayList<>();

    public interface MusicLikeListInterface{
        public void sendLikeValue(String musicid,String musicName, String songpicurl);
    }
    private MusicLikeListInterface musicListInterface;


    public static MusiclikeListFragment newInstance(String serialNo,String dialog_sign) {
        MusiclikeListFragment fragment = new MusiclikeListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("serialNo",serialNo);
        bundle.putString("dialog_sign",dialog_sign);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_music_like_list;
    }

    @Override
    protected void initView() {
        music_like_recyclerview = findViewById(R.id.music_like_Recyclerview);
        music_num = findViewById(R.id.music_num);
        ifscene = dialog_sign.equals("deviceDialog");
        initAdapter();
    }

    @Override
    public void onResume() {
        super.onResume();
        getMusiclike();
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        Bundle arguments = getArguments();
        if (arguments!=null){
             serialNo = arguments.getString("serialNo");
            dialog_sign = arguments.getString("dialog_sign");
        }
        musicListInterface = (MusicLikeListInterface) getActivity();
    }

    @Override
    protected void initData() {
    }

    private void initAdapter() {
        musiclikelistAdapter = new MusiclikelistAdapter();
        music_like_recyclerview.setAdapter(musiclikelistAdapter);
        music_like_recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        if (!ifscene){
            musiclikelistAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                    SwitchMusicOperation(musiclikeListBeans.get(position).getMusicId());
                }
            });
        }

    }

    private void getMusiclike() {
        LLHttpManager.getfavouritemusicinfo(this,serialNo,new HttpCallback<HttpData<MusiclikeListBean>>(this){
            @Override
            public void onStart(Call call) {
                super.onStart(call);
            }

            @Override
            public void onSucceed(HttpData<MusiclikeListBean> result) {
                super.onSucceed(result);
                musiclikeListBeans = result.getData().getMusiclikeLists();
                musiclikelistAdapter.setNewData(musiclikeListBeans);
                if (musiclikeListBeans.size()==0 || musiclikeListBeans==null){
                    music_num.setText("（共"+"0首歌曲）");
                }else {
                    music_num.setText("（共"+musiclikeListBeans.size()+"首歌曲）");
                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                super.onEnd(call);
            }
        });
    }


    //收藏列表适配器
    private class MusiclikelistAdapter extends BaseQuickAdapter<MusiclikeListBean.MusiclikeList, BaseViewHolder>{

        public MusiclikelistAdapter() {
            super(R.layout.item_music_list);
        }

        @Override
        public void setNewData(@Nullable List<MusiclikeListBean.MusiclikeList> data) {
            super.setNewData(data);
        }

        @Override
        public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
            super.onBindViewHolder(holder, position);
            holder.setText(R.id.music_id,position+1+"、");
            TextView music_name = holder.getView(R.id.music_name);
            TextView singer_name = holder.getView(R.id.singer_name);
            if (position==count){
                checkbox_music.setChecked(true);
            }else {
                checkbox_music.setChecked(false);
            }
        }

        @Override
        protected void convert(@NonNull BaseViewHolder holder, MusiclikeListBean.MusiclikeList item) {
            holder.setText(R.id.music_name,item.getMusicName());
            holder.setText(R.id.singer_name,item.getSingerName());
            ImageView music_like = holder.getView(R.id.music_like);
            ImageView music_details = holder.getView(R.id.music_details);
            checkbox_music = holder.getView(R.id.checkbox_music);
            music_like.setVisibility(View.GONE);
            if(ifscene){
                music_details.setVisibility(View.GONE);
                checkbox_music.setVisibility(View.VISIBLE);
                checkbox_music.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        count = holder.getPosition();
                        musicListInterface.sendLikeValue(item.getMusicId(),item.getMusicName(),"");
                        notifyDataSetChanged();
                    }
                });
            }
        }
    }

    private void SwitchMusicOperation(String musicID) {
        Map<String,Object> map=new HashMap<>();
        Map<String,Object> statusMap=new HashMap<>();
        map.put("songid",musicID);
        statusMap.put(STATUS,map);
        LLHttpManager.operationDevice((LifecycleOwner) getContext(),serialNo,statusMap,new HttpCallback<HttpData<Void>>(null){
            @Override
            public void onStart(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity){
                    ((AppActivity) getContext()).showDialog("执行中...");
                }
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
                super.onSucceed(result);
            }

            @Override
            public void onFail(Exception e) {
                ToastUtils.show(R.string.exec_failure);
            }

            @Override
            public void onEnd(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).hideDialog();
                }
            }
        });
    }


}